package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.NextAdapter3;
import com.weiyi.mashangzhu.Landlord.Bean.SetHouseBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.AboutActivity;
import com.weiyi.mashangzhu.Utils.BitmapUtils;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.weiyi.mashangzhu.Weight.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.baidu.location.h.k.m;
import static com.weiyi.mashangzhu.Application.MyApp.id1;
import static com.weiyi.mashangzhu.Application.MyApp.id2;
import static com.weiyi.mashangzhu.Application.MyApp.id3;
import static com.weiyi.mashangzhu.Application.MyApp.img;

public class PublishHouseActivity extends BaseActivity {

    @BindView(R.id.lv)
    MyListView lv;
    List<SetHouseBean> list = new ArrayList<>();
    ArrayList<String> photos = new ArrayList<>();
    ArrayList<String> photos2 = new ArrayList<>();
    List<String> photoslist = new ArrayList<>();
    @BindView(R.id.photo_yes)
    ImageView photoYes;
    @BindView(R.id.photo_count)
    TextView photoCount;
    @BindView(R.id.photo_no)
    RelativeLayout photoNo;
    @BindView(R.id.sv)
    ScrollView sv;
    NextAdapter3 nextAdapter3 = null;
    boolean flag = true;
    List<String> photolist = new ArrayList<>();
    String img1, img2, img3, img4, img5, img6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_house);
        ButterKnife.bind(this);
        sv.smoothScrollTo(0, Integer.MAX_VALUE);
        MyApp.images.clear();

        init();
        list.add(new SetHouseBean("撰写房屋标题和房屋介绍", "未完成"));
        list.add(new SetHouseBean("设置房屋折扣", "未完成"));
        list.add(new SetHouseBean("设置房屋价格", "未完成"));
        list.add(new SetHouseBean("基本信息", "未完成"));
        list.add(new SetHouseBean("账户信息", "未完成"));
        list.add(new SetHouseBean("认证材料", "未完成"));
        list.add(new SetHouseBean("可选配套设施", "未完成"));
        list.add(new SetHouseBean("房源类型", "未完成"));
        list.add(new SetHouseBean("地理位置", "未完成"));
        nextAdapter3 = new NextAdapter3(list, this, "img_have");
        lv.setAdapter(nextAdapter3);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        intent = new Intent(PublishHouseActivity.this, SetHouseTitieActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 1:
                        intent = new Intent(PublishHouseActivity.this, SetDiscountActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 2:
                        intent = new Intent(PublishHouseActivity.this, SetPriceActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 3:
                        intent = new Intent(PublishHouseActivity.this, BaseInfoActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 4:
                        intent = new Intent(PublishHouseActivity.this, AccountInfoActivity_L.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 5:
                        intent = new Intent(PublishHouseActivity.this, ApproveActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 6:
                        intent = new Intent(PublishHouseActivity.this, FacilityActivity_L.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 7:
                        intent = new Intent(PublishHouseActivity.this, HouseTypeActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;
                    case 8:
                        intent = new Intent(PublishHouseActivity.this, AdressActivity.class);
                        intent.putExtra("info","post");
                        PublishHouseActivity.this.startActivityForResult(intent, 0x001);
                        break;

                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x002) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(0).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x003) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(1).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x004) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(2).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x005) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(3).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x006) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(4).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x007) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(5).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x008) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(6).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x009) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(7).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x0010) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(8).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
    }


    @Override
    public void init() {
        super.init();
        if (MyApp.images.size() == 0) {
            photoCount.setVisibility(View.GONE);
            photoNo.setVisibility(View.VISIBLE);
        } else {
            photoCount.setVisibility(View.VISIBLE);
            photoNo.setVisibility(View.GONE);
            File file = new File(MyApp.images.get(0).getUrl());
            Glide.with(this).load(file).into(photoYes);
            photoCount.setText(MyApp.images.size() + "");

        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }


    @OnClick({R.id.back, R.id.add_photo, R.id.submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_photo:
                Intent intent=new Intent(this, PhotoSelectActivity.class);
                intent.putExtra("info","publish");
                startActivity(intent);
                break;
            case R.id.submit:
                if (MyApp.img1 == null) {
                    T.showShort(this, "最少上传4张房源照片");
                } else {
//
                    if (list.get(0).getFinish().equals("已完成")
                            && list.get(1).getFinish().equals("已完成")
                            && list.get(2).getFinish().equals("已完成")
                            && list.get(3).getFinish().equals("已完成")
                            && list.get(4).getFinish().equals("已完成")
                            && list.get(5).getFinish().equals("已完成")
                            && list.get(6).getFinish().equals("已完成")
                            && list.get(7).getFinish().equals("已完成")
                            && list.get(8).getFinish().equals("已完成")
                            ) {
                        LoadingDialog.show(PublishHouseActivity.this);
                        if (MyApp.img.size()==4){
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(this, "token", "") + "")
                                    .addParams("hTitle", MyApp.house_title)
                                    .addParams("hIntro", MyApp.house_content)
                                    .addParams("hPrice", MyApp.price_price)
                                    .addParams("hdeposit", MyApp.price_yajian)
                                    .addParams("hImg1", MyApp.img1)
                                    .addParams("hImg2", MyApp.img2)
                                    .addParams("hImg3", MyApp.img3)
                                    .addParams("hImg4", MyApp.img4)
//                                    .addParams("hImg5", MyApp.img5)
//                                    .addParams("hImg6", MyApp.img6)
                                    .addParams("hAddpeople", MyApp.price_add)
                                    .addParams("hOtherprice", MyApp.price_other)
                                    .addParams("hWedate", MyApp.dis_date)
                                    .addParams("hWeprice", MyApp.dis_price)
                                    .addParams("hSevendiscount", MyApp.dis_7)
                                    .addParams("hThirtydiscount", MyApp.dis_30)
                                    .addParams("hType", MyApp.type)
                                    .addParams("hArea", MyApp.base_area)
                                    .addParams("hRooms", MyApp.base_type_chu)
                                    .addParams("hRoom", MyApp.base_type_shi)
                                    .addParams("hHall", MyApp.base_type_ting)
                                    .addParams("hToilet", MyApp.base_type_wei)
                                    .addParams("hToilettype", MyApp.base_wei)
                                    .addParams("hPeoples", MyApp.base_person)
                                    .addParams("hBedtype", MyApp.base_bed_type)
                                    .addParams("hBedlength", MyApp.base_bed_l)
                                    .addParams("hBedwidth", MyApp.base_bed_w)
                                    .addParams("hBednum", MyApp.base_bed_count)
                                    .addParams("hRealname", MyApp.realname)
                                    .addParams("hMatches", MyApp.facility)
                                    .addParams("hIdcardnum", MyApp.idcard)
                                    .addParams("hIdpic1", MyApp.id1)
                                    .addParams("hIdpic2", MyApp.id2)
                                    .addParams("hIdpic3", MyApp.id3)
                                    .addParams("hReceivables", MyApp.account)
                                    .addParams("hProvince", MyApp.address_province)
                                    .addParams("hCity", MyApp.address_city)
                                    .addParams("hDistrict", MyApp.address_district)
                                    .addParams("hAddress", MyApp.address_address)
                                    .addParams("hBuilding", MyApp.address_loudong)
                                    .addParams("hFloor", MyApp.address_louceng)
                                    .addParams("hRoomnum", MyApp.address_roomnum)
                                    .addParams("hPhone", MyApp.phone)
                                    .url(API.BaseApi_L + API.AddHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
//                                            T.showShort(PublishHouseActivity.this, "网络连接失败，请检查一下网络设置");
                                            T.showShort(PublishHouseActivity.this, "请完善资料后重试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                T.showShort(PublishHouseActivity.this, jsonObject.getString("msg"));
                                                if (jsonObject.getInt("status") == 0) {
                                                    T.showShort(PublishHouseActivity.this, "后台审核中...");
                                                    finish();
                                                }

                                            } catch (JSONException e) {

                                            }

                                        }
                                    });
                        }else if (img.size()==5){
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(this, "token", "") + "")
                                    .addParams("hTitle", MyApp.house_title)
                                    .addParams("hIntro", MyApp.house_content)
                                    .addParams("hPrice", MyApp.price_price)
                                    .addParams("hdeposit", MyApp.price_yajian)
                                    .addParams("hImg1", MyApp.img1)
                                    .addParams("hImg2", MyApp.img2)
                                    .addParams("hImg3", MyApp.img3)
                                    .addParams("hImg4", MyApp.img4)
                                    .addParams("hImg5", MyApp.img5)
                                    .addParams("hAddpeople", MyApp.price_add)
                                    .addParams("hOtherprice", MyApp.price_other)
                                    .addParams("hWedate", MyApp.dis_date)
                                    .addParams("hWeprice", MyApp.dis_price)
                                    .addParams("hSevendiscount", MyApp.dis_7)
                                    .addParams("hThirtydiscount", MyApp.dis_30)
                                    .addParams("hType", MyApp.type)
                                    .addParams("hArea", MyApp.base_area)
                                    .addParams("hRooms", MyApp.base_type_chu)
                                    .addParams("hRoom", MyApp.base_type_shi)
                                    .addParams("hHall", MyApp.base_type_ting)
                                    .addParams("hToilet", MyApp.base_type_wei)
                                    .addParams("hToilettype", MyApp.base_wei)
                                    .addParams("hPeoples", MyApp.base_person)
                                    .addParams("hBedtype", MyApp.base_bed_type)
                                    .addParams("hBedlength", MyApp.base_bed_l)
                                    .addParams("hBedwidth", MyApp.base_bed_w)
                                    .addParams("hBednum", MyApp.base_bed_count)
                                    .addParams("hRealname", MyApp.realname)
                                    .addParams("hMatches", MyApp.facility)
                                    .addParams("hIdcardnum", MyApp.idcard)
                                    .addParams("hIdpic1", MyApp.id1)
                                    .addParams("hIdpic2", MyApp.id2)
                                    .addParams("hIdpic3", MyApp.id3)
                                    .addParams("hReceivables", MyApp.account)
                                    .addParams("hProvince", MyApp.address_province)
                                    .addParams("hCity", MyApp.address_city)
                                    .addParams("hDistrict", MyApp.address_district)
                                    .addParams("hAddress", MyApp.address_address)
                                    .addParams("hBuilding", MyApp.address_loudong)
                                    .addParams("hFloor", MyApp.address_louceng)
                                    .addParams("hRoomnum", MyApp.address_roomnum)
                                    .addParams("hPhone", MyApp.phone)
                                    .url(API.BaseApi_L + API.AddHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
//                                            T.showShort(PublishHouseActivity.this, "网络连接失败，请检查一下网络设置");
                                            T.showShort(PublishHouseActivity.this, "请完善资料后重试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                T.showShort(PublishHouseActivity.this, jsonObject.getString("msg"));
                                                if (jsonObject.getInt("status") == 0) {
                                                    T.showShort(PublishHouseActivity.this, "后台审核中...");
                                                    finish();
                                                }

                                            } catch (JSONException e) {

                                            }

                                        }
                                    });
                        }else if (img.size()==6){
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(this, "token", "") + "")
                                    .addParams("hTitle", MyApp.house_title)
                                    .addParams("hIntro", MyApp.house_content)
                                    .addParams("hPrice", MyApp.price_price)
                                    .addParams("hdeposit", MyApp.price_yajian)
                                    .addParams("hImg1", MyApp.img1)
                                    .addParams("hImg2", MyApp.img2)
                                    .addParams("hImg3", MyApp.img3)
                                    .addParams("hImg4", MyApp.img4)
                                    .addParams("hImg5", MyApp.img5)
                                    .addParams("hImg6", MyApp.img6)
                                    .addParams("hAddpeople", MyApp.price_add)
                                    .addParams("hOtherprice", MyApp.price_other)
                                    .addParams("hWedate", MyApp.dis_date)
                                    .addParams("hWeprice", MyApp.dis_price)
                                    .addParams("hSevendiscount", MyApp.dis_7)
                                    .addParams("hThirtydiscount", MyApp.dis_30)
                                    .addParams("hType", MyApp.type)
                                    .addParams("hArea", MyApp.base_area)
                                    .addParams("hRooms", MyApp.base_type_chu)
                                    .addParams("hRoom", MyApp.base_type_shi)
                                    .addParams("hHall", MyApp.base_type_ting)
                                    .addParams("hToilet", MyApp.base_type_wei)
                                    .addParams("hToilettype", MyApp.base_wei)
                                    .addParams("hPeoples", MyApp.base_person)
                                    .addParams("hBedtype", MyApp.base_bed_type)
                                    .addParams("hBedlength", MyApp.base_bed_l)
                                    .addParams("hBedwidth", MyApp.base_bed_w)
                                    .addParams("hBednum", MyApp.base_bed_count)
                                    .addParams("hRealname", MyApp.realname)
                                    .addParams("hMatches", MyApp.facility)
                                    .addParams("hIdcardnum", MyApp.idcard)
                                    .addParams("hIdpic1", MyApp.id1)
                                    .addParams("hIdpic2", MyApp.id2)
                                    .addParams("hIdpic3", MyApp.id3)
                                    .addParams("hReceivables", MyApp.account)
                                    .addParams("hProvince", MyApp.address_province)
                                    .addParams("hCity", MyApp.address_city)
                                    .addParams("hDistrict", MyApp.address_district)
                                    .addParams("hAddress", MyApp.address_address)
                                    .addParams("hBuilding", MyApp.address_loudong)
                                    .addParams("hFloor", MyApp.address_louceng)
                                    .addParams("hRoomnum", MyApp.address_roomnum)
                                    .addParams("hPhone", MyApp.phone)
                                    .url(API.BaseApi_L + API.AddHouse_Api)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
//                                            T.showShort(PublishHouseActivity.this, "网络连接失败，请检查一下网络设置");
                                            T.showShort(PublishHouseActivity.this, "请完善资料后重试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(PublishHouseActivity.this);
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                T.showShort(PublishHouseActivity.this, jsonObject.getString("msg"));
                                                if (jsonObject.getInt("status") == 0) {
                                                    T.showShort(PublishHouseActivity.this, "后台审核中...");
                                                    finish();
                                                }

                                            } catch (JSONException e) {

                                            }

                                        }
                                    });
                        }


                    } else {
                        T.showShort(this, "请完善资料后提交审核");
                    }
                    break;
                }
        }
    }
}
